
/*  引入相关的文件配置路由 */
import Vue from 'vue'
import VueRouter from 'vue-router'
/*  使用vue路由方法 */
Vue.use(VueRouter)
/*  创建路由对象 */
let router = new VueRouter({
  /*  配置路由 */
  routes: [
    //默认打开页--首页
    {
      name: 'default',  //默认
      path: '/',
      redirect: { name: 'index' }
    },
    //首页
    {
      name: 'index',
      path: '/index',
      component: () => import('@/views/index.vue'),
      meta: {
        title: "黑马头条"
      }
    },
    // 登录
    {
      name: 'login',
      path: '/login',
      component: () => import('@/views/login.vue'),
      meta: {
        title: "登录"
      }
    },

    //注册
    {
      name: 'register',
      path: '/register',
      component: () => import('@/views/register.vue'),
      meta: {
        title: "注册"
      }
    },
    //个人中心
    {
      name: 'mypersonal',
      path: '/mypersonal/:id',
      component: () => import('@/views/mypersonal.vue'),
      meta: {
        title: "个人中心"
      }
    },
    //编辑个人信息
    {
      name: 'myeditprofile',
      path: '/myeditprofile/:id',
      component: () => import('@/views/myeditprofile.vue'),
      meta: {
        title: "个人信息"
      }
    },

    //文章详情
    {
      name: 'articaldetail',
      path: '/articaldetail/:id',
      component: () => import('@/views/articaldetail.vue'),
      meta: {
        title: "文章详情"
      }
    },
    //我的跟帖
    {
      name: 'mykeeps',
      path: '/mykeeps',
      component: () => import('@/views/mykeeps.vue'),
      meta: {
        title: "我的跟帖"
      }
    },
    //我的关注
    {
      name: 'myfocus',
      path: '/myfocus',
      component: () => import('@/views/myfocus.vue'),
      meta: {
        title: "我的关注"
      }

    },
    // 新闻列表
    {
      name: 'mynewlists',
      path: '/mynewlists',
      component: () => import('@/views/mynewlists.vue'),
      meta: {
        title: "新闻列表"
      }
    },
    // 精彩跟帖
    {
      name: 'mythread',
      path: '/mythread/:id',
      component: () => import('@/views/mythread.vue'),
      meta: {
        title: "精彩跟帖"
      }
    },
    // 栏目管理
    {
      name: 'mycolumn',
      path: '/mycolumn',
      component: () => import('@/views/mycolumn.vue'),
      meta: {
        title: "栏目管理"
      }
    },
    // 搜索
    {
      name: 'mysearch',
      path: '/mysearch',
      component: () => import('@/views/mysearch.vue'),
      meta: {
        title: "搜索"
      }
    },
  ]
})


// 添加全局的导航守卫--前置守卫,在跳转前触发
//他的作用是限制用户的路由跳转, 路由能不能跳转,先经过守卫进行判断并处理
router.beforeEach((to, from, next) => {
  let arr = ['/persnal', '/myeditprofile']
  // for (let i = 0; i < arr.length; i++) {
  //我们需要判断一下用户当前目标路由是否是需要验证的路由
  if (arr.indexOf(to.path) !== -1) {
    //判断用户是否有合法的登录状态,如果有则继续跳转,如果没有则重定向到登录
    let token = localStorage.getItem('heima-headline')
    if (token) {
      next()
    } else {
      //next的参数与之前的$router.push方法的参数一致
      next({
        name: 'login'
      })
    }
  } else {
    next()
  }
  // }
})

router.afterEach((to, from) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
})


/* 暴露路由路由配置 */
export default router